/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  DateSelectUtil.java   
 * @Package com.swift.maintenance.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-30 下午3:28:44   
 * @version V1.0     
 */
package com.swift.maintenance.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.swift.maintenance.R;
import com.swift.utils.DateUtils;
import com.swift.utils.DeviceUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: DateSelectUtil
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-30 下午3:28:44
 * 
 */
public class DateSelectUtil {
	volatile static DateSelectUtil instence;
	View view;

	public static DateSelectUtil getInstance() {
		synchronized (DateSelectUtil.class) {
			if (null == instence) {
				instence = new DateSelectUtil();
			}
		}
		return instence;
	}

	public interface OnDateSelectSureListener {
		void sure();
	}

	@SuppressLint("NewApi")
	public void selectdate(View touchview, int touchviewid,
			final EditText etEditText, String title,
			AlertDialog.Builder builder, View flateView,
			final OnDateSelectSureListener listener) {
		// View view = View.inflate(this, R.layout.date_time_dialog, null);
		view = flateView;
		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.date_picker);

		datePicker.setCalendarViewShown(false);
		final TimePicker timePicker = (TimePicker) view
				.findViewById(R.id.time_picker);
		builder.setView(view);

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH), null);

		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(Calendar.MINUTE);

		resizePikcer(datePicker);// 调整datepicker大小
		resizePikcer(timePicker);// 调整timepicker大小

		if (touchview.getId() == touchviewid) {
			final int inType = etEditText.getInputType();
			etEditText.setInputType(InputType.TYPE_NULL);
			etEditText.setInputType(inType);
			etEditText.setSelection(etEditText.getText().length());

			builder.setTitle(title);
			builder.setPositiveButton("确  定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							StringBuffer sb = new StringBuffer();
							sb.append(String.format("%d-%02d-%02d",
									datePicker.getYear(),
									datePicker.getMonth() + 1,
									datePicker.getDayOfMonth()));
							sb.append("  ");
							sb.append(String.format("%02d:%02d",
									timePicker.getCurrentHour(),
									timePicker.getCurrentMinute()));
							Log.e("data:", sb.toString());
							Date select = DateUtils.getDateFromTime(sb
									.toString());
							if (select != null && new Date().after(select)) {
								ToastUtil.getInstances().showMsg(
										"选择的时间不能比当前时间早");
								return;
							}
							etEditText.setText(sb);

							dialog.dismiss();
							dialog = null;
							if (null != listener)
								listener.sure();
						}
					});

		}
		Dialog dialog = builder.create();
		dialog.show();
	}

	@SuppressLint("NewApi")
	public void selectdate(View touchview, int touchviewid,
			final TextView textView, String title, AlertDialog.Builder builder,
			View flateView, final OnDateSelectSureListener listener) {
		// View view = View.inflate(this, R.layout.date_time_dialog, null);
		view = flateView;
		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.date_picker);

		datePicker.setCalendarViewShown(false);
		final TimePicker timePicker = (TimePicker) view
				.findViewById(R.id.time_picker);
		builder.setView(view);

		Calendar cal = Calendar.getInstance();
		if (TextUtils.isEmpty(textView.getText())) {
			cal.setTimeInMillis(System.currentTimeMillis());
		} else {
			cal.setTimeInMillis(DateUtils.fmtStrToDate(
					textView.getText().toString(), DateUtils.DATEFORMAT)
					.getTime());
		}

		datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH), null);

		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(cal.get(Calendar.MINUTE));

		resizePikcer(datePicker);// 调整datepicker大小
		resizePikcer(timePicker);// 调整timepicker大小

		if (touchview.getId() == touchviewid) {
			builder.setTitle(title);
			builder.setPositiveButton("确  定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							StringBuffer sb = new StringBuffer();
							sb.append(String.format("%d-%02d-%02d",
									datePicker.getYear(),
									datePicker.getMonth() + 1,
									datePicker.getDayOfMonth()));
							sb.append(" ");
							sb.append(String.format("%02d:%02d",
									timePicker.getCurrentHour(),
									timePicker.getCurrentMinute()));
							Log.e("data:", sb.toString());
							textView.setText(sb);
							dialog.dismiss();
							dialog = null;
							if (null != listener)
								listener.sure();
						}
					});
			builder.setNegativeButton("取  消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							try {
								dialog.dismiss();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
		}
		Dialog dialog = builder.create();
		dialog.show();
	}

	/**
	 * 调整FrameLayout大小
	 * 
	 * @param tp
	 */
	private void resizePikcer(FrameLayout tp) {
		List<NumberPicker> npList = findNumberPicker(tp);
		for (NumberPicker np : npList) {
			resizeNumberPicker(np);
		}
	}

	/**
	 * 得到viewGroup里面的numberpicker组件
	 * 
	 * @param viewGroup
	 * @return
	 */
	private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
		List<NumberPicker> npList = new ArrayList<NumberPicker>();
		View child = null;
		if (null != viewGroup) {
			for (int i = 0; i < viewGroup.getChildCount(); i++) {
				child = viewGroup.getChildAt(i);
				if (child instanceof NumberPicker) {
					npList.add((NumberPicker) child);
				} else if (child instanceof LinearLayout) {
					List<NumberPicker> result = findNumberPicker((ViewGroup) child);
					if (result.size() > 0) {
						return result;
					}
				}
			}
		}
		return npList;
	}

	/*
	 * 调整numberpicker大小
	 */
	private void resizeNumberPicker(NumberPicker np) {
		LayoutParams params = new LayoutParams((int) (45 * DeviceUtil.getInstance().getBiLi()),
				LayoutParams.WRAP_CONTENT);
		params.setMargins(10, 0, 10, 0);
		np.setLayoutParams(params);
	}
}
